#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Controls.WpfPropertyGrid;
using ReplicationExplorer.DomainModel.Infrastructure;
using ReplicationExplorer.DomainModel.Subscriber.Entities;

namespace ReplicationExplorer.DomainModel.Subscriber.Models
{
    [CategoryOrder("Subscription", 0)]
    [CategoryOrder("Subscription stats",1)]
    [CategoryOrder("Subscription info",2)]
    public class SubscriptionViewModel : TreeViewItemViewModel
    {
        private readonly Subscription _subscription;
        private readonly SubscriptionFolderViewModel _parent;

        public SubscriptionViewModel(Subscription subscription, SubscriptionFolderViewModel parent, bool lazyLoadChildren)
            : base(parent, lazyLoadChildren)
        {
            _subscription = subscription;
            _parent = parent;
        }

        public override string ModelViewIcon
        {
            get
            {
                return "Subscription.png";
            }
        }

        public override string ModelViewName
        {
            get
            {
                return "Subscription";
            }
        }

        [Category("Subscription")]
        [Description("Subscriber server name")]
        [DisplayName(@"Subscriber")]
        [ReadOnly(true)]
        [PropertyOrder(0)]
        public string Name
        {
            get { return _subscription.Subscriber; }
        }

        [Category("Subscription")]
        [Description("Subscription database name")]
        [DisplayName(@"Subscription database")]
        [ReadOnly(true)]
        [PropertyOrder(1)]
        public string SubscriptionDatabase
        {
            get { return _subscription.SubscriptionDatabase; }
        }

        [Category("Subscription")]
        [Description("Subscription type")]
        [DisplayName(@"Subscription type")]
        [ReadOnly(true)]
        [PropertyOrder(2)]
        public string SubscriptionType
        {
            get {
                string type;
                switch(_subscription.Type)
                {
                    case 0:
                        type = "Push";
                        break;
                    case 1:
                        type = "Pull";
                        break;
                    case 2:
                        type = "Anonymous";
                        break;
                    default:
                        type = "N/A";
                        break;
                }

                return type;
            }
        }

        [Category("Subscription stats")]
        [Description("Status")]
        [DisplayName(@"Status")]
        [ReadOnly(true)]
        [PropertyOrder(0)]
        public string Status
        {
            get { return UI.StringConverter.ConvertPublisherStatus(_subscription.Status); ; }
        }

        [Category("Subscription stats")]
        [Description("Warning")]
        [DisplayName(@"Warning")]
        [ReadOnly(true)]
        [PropertyOrder(1)]
        public string Warning
        {
            get { return UI.StringConverter.ConvertPublisherWarning(_subscription.Warning); ; }
        }

        [Category("Subscription info")]
        [Description("Subscription may expire in next, if synchronization will not occur.")]
        [DisplayName(@"Subscription may expires in")]
        [ReadOnly(true)]
        [PropertyOrder(3)]
        public string TimeToSubscriptionExpiry
        {
            get
            {
                var hours = -_subscription.TimeToExpiration;
                return hours.HasValue ? UI.StringConverter.ConvertToHoursOrDays(hours.Value) : "N/A";
            }
        }
        
        [Category("Subscription info")]
        [Description("Distribution agent Sql Server Agent Job name.")]
        [DisplayName(@"Distribution agent")]
        [ReadOnly(true)]
        [PropertyOrder(4)]
        public string DistributionAgentName
        {
            get
            {
                return _subscription.DistributionAgentName;
            }
        }

        [Category("Subscription info")]
        [Description("Last time Distribution agent run against this subscription.")]
        [DisplayName(@"Last time Distribution agent run")]
        [ReadOnly(true)]
        [PropertyOrder(5)]
        public string LastTimeDistributionAgentRun
        {
            get
            {
                DateTime? lastRun = _subscription.LastTimeDistributionAgentRun;
                return lastRun.HasValue ? lastRun.Value.ToString(CultureInfo.CurrentUICulture) : "N/A";
            }
        }

        [Category("Subscription info")]
        [Description("Merge agent Sql Server Agent Job name.")]
        [DisplayName(@"Merge agent")]
        [ReadOnly(true)]
        [PropertyOrder(6)]
        public string MergeAgentName
        {
            get
            {
                return _subscription.MergeAgentName;
            }
        }


    }
}